# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=mono
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_gitcommit=fe64a4765e6d1dbb41d5c86708fcb02aa519247a
pkgver=6.4.0.198
pkgrel=1
pkgdesc='Free implementation of the .NET platform including runtime and compiler (mingw-w64)'
url='https://www.mono-project.com/'
arch=('any')
license=('GPL' 'LGPL2.1' 'MPL')
depends=("${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-libgdiplus"
         "${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-ca-certificates")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-mono"
             "git")
provides=("${MINGW_PACKAGE_PREFIX}-monodoc")
conflicts=("${MINGW_PACKAGE_PREFIX}-monodoc")
source=(${_realname}::"git+https://github.com/mono/mono#commit=${_gitcommit}"
        #https://github.com/mono/mono/archive/mono-${pkgver}.tar.gz
        git+https://github.com/mono/aspnetwebstack
        git+https://github.com/mono/Newtonsoft.Json
        git+https://github.com/mono/cecil
        git+https://github.com/mono/rx
        git+https://github.com/mono/ikvm-fork
        git+https://github.com/mono/ikdasm
        git+https://github.com/mono/reference-assemblies
        git+https://github.com/mono/NUnitLite
        git+https://github.com/mono/NuGet.BuildTasks
        git+https://github.com/mono/boringssl
        git+https://github.com/mono/corefx
        git+https://github.com/mono/bockbuild
        git+https://github.com/mono/linker
        git+https://github.com/mono/roslyn-binaries
        git+https://github.com/mono/corert
        git+https://github.com/mono/xunit-binaries
        git+https://github.com/mono/api-doc-tools
        git+https://github.com/mono/api-snapshot
        0001-mono-5.4.1.7-fix-build.patch
        0002-mono-5.4.1.7-avoid-appending-prefix.patch
        0003-mono-5.4.1.7-fix-runtime.patch)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '04ab8ba5c7ad451b9e9d4bd76a4850afb790212ba73ac65e1e20f05c11de9924'
            '772472f89f6884bf90159312a699a6d9768908c217edecd8bace08ab7ca7dceb'
            '884d39a412231939ed48b6009fea26ae16a9c1b69f309fa289db071ec0002f28')

pkgver() {
  cd ${srcdir}/${_realname}
  git describe --always --tags | sed 's/^v//;s/^mono-//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd ${srcdir}/${_realname}
  git submodule init
  git config submodule."external/aspnetwebstack".url "${srcdir}/aspnetwebstack"
  git config submodule."external/Newtonsoft.Json".url "${srcdir}/Newtonsoft.Json"
  git config submodule."external/cecil".url "${srcdir}/cecil"
  git config submodule."external/rx".url "${srcdir}/rx"
  git config submodule."external/ikvm".url "${srcdir}/ikvm-fork"
  git config submodule."external/ikdasm".url "${srcdir}/ikdasm"
  git config submodule."external/reference-assemblies".url "${srcdir}/reference-assemblies"
  git config submodule."external/nunit-lite".url "${srcdir}/NUnitLite"
  git config submodule."external/nuget-buildtasks".url "${srcdir}/NuGet.BuildTasks"
  git config submodule."external/cecil-legacy".url "${srcdir}/cecil"
  git config submodule."external/boringssl".url "${srcdir}/boringssl"
  git config submodule."external/corefx".url "${srcdir}/corefx"
  git config submodule."external/bockbuild".url "${srcdir}/bockbuild"
  git config submodule."external/linker".url "${srcdir}/linker"
  git config submodule."external/roslyn-binaries".url "${srcdir}/roslyn-binaries"
  git config submodule."external/corert".url "${srcdir}/corert"
  git config submodule."external/xunit-binaries".url "${srcdir}/xunit-binaries"
  git config submodule."external/api-doc-tools".url "${srcdir}/api-doc-tools"
  git config submodule."external/api-snapshot".url "${srcdir}/api-snapshot"
  git submodule update --recursive

  patch -Np1 -i "${srcdir}/0001-mono-5.4.1.7-fix-build.patch"
  patch -Np1 -i "${srcdir}/0002-mono-5.4.1.7-avoid-appending-prefix.patch"
  patch -Np1 -i "${srcdir}/0003-mono-5.4.1.7-fix-runtime.patch"

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  cp -R ${srcdir}/${_realname}/. "${srcdir}"/build-${CARCH}
  
  LDFLAGS+=" -fstack-protector"
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-boehm \
    --enable-static \
    --enable-shared \
    --with-mcs-docs=no \
    --with-libgdiplus=${MINGW_PREFIX}
  make
  make -C mcs/jay
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}" install
  make -C mcs/jay DESTDIR="${pkgdir}" prefix=${MINGW_PREFIX} install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # Fix paths in bin directory
  for f in ${pkgdir}${MINGW_PREFIX}/bin/{al,al2,caspol,cccheck,ccrewrite,cert2spc,certmgr,cert-sync,chktrust,crlupdate,csc,csharp,csi,disco,dmcs,dtd2rng,dtd2xsd,gacutil,gacutil2,genxs,httpcfg,ikdasm,ilasm,illinkanalyzer,installvst,lc,macpack,makecert,mconfig,mcs,mdassembler,mdbrebase,mdoc,mdoc-assemble,mdoc-export-html,mdoc-export-msxdoc,mdoc-update,mdoc-validate,mdvalidater,mkbundle,mod,mono-api-html,mono-api-info,mono-cil-strip,mono-configuration-crypto,monodocer,monodocs2html,monodocs2slashdoc,mono-find-provides,mono-find-requires,monolinker,monop,monop2,mono-package-runtime,mono-service,mono-service2,mono-shlib-cop,mono-symbolicate,mono-test-install,mono-xmltool,mozroots,pdb2mdb,permview,peverify,resgen,resgen2,secutil,setreg,sgen,signcode,sn,soapsuds,sqlmetal,sqlsharp,svcutil,vbc,wsdl,wsdl2,xbuild,xsd}; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${f}
  done

  mv  ${pkgdir}${MINGW_PREFIX}/bin/mono{,.exe}
}
